package com.shxy.rlzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shxy.rlzy.entity.Salary;
import com.shxy.rlzy.entity.SalaryItem;
import com.shxy.rlzy.entity.SalaryTask;
import com.shxy.rlzy.entity.SalaryTaskDepart;
import com.shxy.rlzy.mapper.SalaryItemMapper;
import com.shxy.rlzy.mapper.SalaryMapper;
import com.shxy.rlzy.mapper.SalaryTaskDepartMapper;
import com.shxy.rlzy.mapper.SalaryTaskMapper;
import com.shxy.rlzy.service.ISalaryTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanhao
 * @since 2022-03-19
 */
@Service
public class SalaryTaskServiceImpl extends ServiceImpl<SalaryTaskMapper, SalaryTask> implements ISalaryTaskService {

    @Autowired
    private SalaryTaskMapper salaryTaskMapper;
    @Autowired
    private SalaryTaskDepartMapper salaryTaskDepartMapper;
    @Autowired
    private SalaryItemMapper salaryItemMapper;
    @Autowired
    private SalaryMapper salaryMapper;

    @Override
    @Transactional
    public SalaryTask addNew(SalaryTask st, List<SalaryTaskDepart> stds) {
        // 1、添加任务
        int sn = salaryTaskMapper.insert(st);
        // 2、循环添加部门任务
        if(sn > 0) {
            stds.forEach(std -> {
                std.setTaskId(st.getId());
                salaryTaskDepartMapper.insert(std);
                // 3、循环添加各部门职工任务
                List<Salary> salaries = getDepartSalaries(std.getDepartId());
                // 3.1 查询个人工资列表
                List<SalaryItem> sis = salaries.stream().map(salary -> {
                    SalaryItem si = new SalaryItem();   // 创建工资项目
                    BeanUtils.copyProperties(salary, si);   // 复制参数
                    si.setTaskId(st.getId());           // 设置任务id
                    si.setTaskDepartId(std.getId());    // 设置部门任务id
                    si.setSalaryId(salary.getId());     // 设置员工工资模板id
                    return si;
                }).collect(Collectors.toList());
                // 3.2 保存个人工资项目
                sis.forEach(salaryItem -> {
                    salaryItemMapper.insert(salaryItem);
                });
            });
        }
        return st;
    }

    /**
     * 获取部门人员工资列表
     * @param departId
     * @return
     */
    private List<Salary> getDepartSalaries(long departId) {
        return salaryMapper.queryList(departId, "", "");
    }

    @Override
    @Transactional
    public void deleteTaskById(long id) {
        // 1、删除员工记录
        QueryWrapper<SalaryItem> qwsi = new QueryWrapper<>();
        qwsi.eq("task_id", id);
        salaryItemMapper.delete(qwsi);

        // 2、删除部门记录
        QueryWrapper<SalaryTaskDepart> qwstd = new QueryWrapper<>();
        qwstd.eq("task_id", id);
        salaryTaskDepartMapper.delete(qwstd);

        // 3、删除任务
        salaryTaskMapper.deleteById(id);
    }

    @Override
    public void changeStatuById(long taskId, int statu) {
        // 1、修改任务状态
        salaryTaskMapper.changeStatuById(taskId, statu);
        // 2、修改部门任务状态
        salaryTaskDepartMapper.changeStatusOfTask(taskId, statu);
        // 3、修改个人任务状态
        salaryItemMapper.changeStatusOfTask(taskId, statu);
    }

    @Override
    public Page<SalaryTask> pageOfStatu(Page<SalaryTask> page, int statu) {
        // 查询条件
        QueryWrapper<SalaryTask> tqw = new QueryWrapper<>();
        tqw.eq("statu", statu);
        return this.page(page, tqw);
    }

    @Override
    public List<SalaryTask> getPagedList(int current, int size) {
        current = current < 1 ? 1 : current;
        return salaryTaskMapper.getPagedList((current-1)*size, size);
    }
}
